package client

import (
	"gitee.com/jnshao/go-start/projects/crawler/engine"
	"net/rpc"

	"gitee.com/jnshao/go-start/projects/crawler_distributed/config"
	"gitee.com/jnshao/go-start/projects/crawler_distributed/worker"
)

// 返回处理方法，供多个协程调用
func CreateProcess(clientChan chan *rpc.Client) func(request engine.Request) (engine.ParserResult, error) {
	return func(request engine.Request) (engine.ParserResult, error) {
		r := worker.SerialzeRequest(request)
		var result worker.ParserResult
		client := <-clientChan
		err := client.Call(config.CrawlServiceRpc, r, &result)
		if err != nil {
			return engine.ParserResult{}, err
		}
		return worker.DeserialzeResult(result), nil
	}
}
